layui.use(['table', 'admin', 'ax', 'func'], function () {
    var $ = layui.$;
    var table = layui.table;
    var $ax = layui.ax;
    var admin = layui.admin;
    var func = layui.func;

    /**
     * 管理
     */
    var BtyWmtxl = {
        tableId: "btyWmtxlTable"
    };

    /**
     * 初始化表格的列
     */
    BtyWmtxl.initColumn = function () {
        return [[
            {type: 'checkbox',LAY_CHECKED:true,hide:true},
            {field: 'id', sort: true,hide:true,title: ''},
           // {align: 'center', type: "numbers", title: '序号',width: 50,},
            {field: 'elm', sort: true, title: '饿了么店名'},
            {field: 'mt', sort: true, title: '美团店名'},
            //{field: 'mttg', sort: true, title: '美团团购店名'},
            {field: 'yaoma', sort: true, title: '妖码'},
            //{field: 'flag', sort: true, title: '标志'},
            //{align: 'center', toolbar: '#tableBar', title: '操作'}
        ]];
    };

    /**
     * 点击查询按钮
     */
    BtyWmtxl.search = function () {
        var queryData = {};

        queryData['elm'] = $('#elm').val();
        queryData['mt'] = $('#mt').val();
        queryData['yaoma'] = $('#yaoma').val();

        table.reload(BtyWmtxl.tableId, {
            where: queryData, page: {curr: 1}
        });
    };

    /**
     * 弹出添加对话框
     */
    BtyWmtxl.openAddDlg = function () {
        func.open({
            title: '添加',
            content: Feng.ctxPath + '/btyWmtxl/add',
            tableId: BtyWmtxl.tableId
        });
    };

     /**
      * 点击编辑
      *
      * @param data 点击按钮时候的行数据
      */
      BtyWmtxl.openEditDlg = function (data) {
          func.open({
              title: '修改',
              content: Feng.ctxPath + '/btyWmtxl/edit?=' + data.id,
              tableId: BtyWmtxl.tableId
          });
      };


    /**
     * 导出excel按钮
     */
    BtyWmtxl.exportExcel = function () {
        //获取导出的数据
        var ajax = new $ax(Feng.ctxPath + "/btyWmtxl/exportList");
        var elm = $('#elm').val();
        var mt = $('#mt').val();
        var yaoma = $('#yaoma').val();
        ajax.set("elm",elm);
        ajax.set("mt",mt);
        ajax.set("yaoma",yaoma);
        var result = ajax.start();
        var checkRows = table.checkStatus(BtyWmtxl.tableId);
        if (checkRows.data.length === 0) {
            Feng.error("请选择要导出的数据");
        } else {
            //身份证银行卡导出excel科学计数法问题
            var lists=result;
            for(index in lists){
                var yaoma= lists[index].yaoma;
                lists[index].yaoma= yaoma+"\t";
            }
            table.exportFile(tableResult.config.id, lists, 'xls');
        }
    };

    /**
     * 点击删除
     *
     * @param data 点击按钮时候的行数据
     */
    BtyWmtxl.onDeleteItem = function (data) {
        var operation = function () {
            var ajax = new $ax(Feng.ctxPath + "/btyWmtxl/delete", function (data) {
                Feng.success("删除成功!");
                table.reload(BtyWmtxl.tableId);
            }, function (data) {
                Feng.error("删除失败!" + data.responseJSON.message + "!");
            });
            ajax.set("", data.id);
            ajax.start();
        };
        Feng.confirm("是否删除?", operation);
    };

    // 渲染表格
    var tableResult = table.render({
        elem: '#' + BtyWmtxl.tableId,
        url: Feng.ctxPath + '/btyWmtxl/list',
        page: true,
        height: "full-158",
        cellMinWidth: 100,
        cols: BtyWmtxl.initColumn()
    });

    // 搜索按钮点击事件
    $('#btnSearch').click(function () {
        BtyWmtxl.search();
    });

    // 添加按钮点击事件
    $('#btnAdd').click(function () {

    BtyWmtxl.openAddDlg();

    });

    // 导出excel
    $('#btnExp').click(function () {
        BtyWmtxl.exportExcel();
    });

    // 工具条点击事件
    table.on('tool(' + BtyWmtxl.tableId + ')', function (obj) {
        var data = obj.data;
        var layEvent = obj.event;

        if (layEvent === 'edit') {
            BtyWmtxl.openEditDlg(data);
        } else if (layEvent === 'delete') {
            BtyWmtxl.onDeleteItem(data);
        }
    });
});
